package de.jmda.gen.java.naming;

/**
 * This singleton provides {@link ImportManager} instances in the following
 * manner:
 * <p>
 * {@link #demandImportManager(String)} returns a non null {@link
 * ImportManager}. If not null {@link #importManager} will be returned,
 * otherwise a new {@link ImportManager} instance will be assigned to it before
 * it is returned.
 * <p>
 * {@link #setImportManager(String)} assigns a new {@link ImportManager} to
 * {@link #importManager} and returns the previously assigned value. This way
 * callers of {@link #setImportManager(String)} can restore the {@link
 * ImportManagerProvider} when they are done.
 *
 * @author roger.wegner
 */
public class ImportManagerProvider
{
	private final static ImportManagerProvider INSTANCE =
			new ImportManagerProvider();

	private ImportManager importManager;

	private ImportManagerProvider()
	{
		super();
	}

	public static ImportManager demandImportManager(String packagename)
	{
		if (INSTANCE.importManager == null)
		{
			INSTANCE.importManager = new ImportManager(packagename);
		}
		
		return INSTANCE.importManager;
	}
	
	public static ImportManager demandImportManager()
	{
		if (INSTANCE.importManager == null)
		{
			INSTANCE.importManager = new ImportManager();
		}
		
		return INSTANCE.importManager;
	}
	
	public static ImportManager setImportManager(String packagename)
	{
		ImportManager result = INSTANCE.importManager;
		
		INSTANCE.importManager = new ImportManager(packagename);
		
		return result;
	}
}